﻿/*
 * Erstellt mit SharpDevelop.
 * Benutzer: Kai Patrick
 * Datum: 19.03.2008
 * Zeit: 10:30
 * 
 * Sie können diese Vorlage unter Extras > Optionen > Codeerstellung > Standardheader ändern.
 */

using System;
using System.Collections.Generic;
using System.Xml;

namespace Gamadus.Designer
{
	public class Action
	{
		public ActionPrototype Prototype;
		
		public string Description
		{
			get
			{
				return String.Format(Prototype.DescriptionPrototype, ParamDisplayValues);
			}
		}
		
		public string[] ParamDisplayValues
		{
			get
			{
				string[] array = new string[paramList.Count];
				int i = 0;
				foreach(ActionParam param in paramList.Values)
				{
					array[i++] = param.DisplayValue;
				}
				return array;
			}
		}
		
		public string[] ParamCodeValues
		{
			get
			{
				string[] array = new string[paramList.Count];
				int i = 0;
				foreach(ActionParam param in paramList.Values)
				{
					array[i++] = param.CodeValue;
				}
				return array;
			}
		}
		
		Dictionary<string,ActionParam> paramList = new Dictionary<string,ActionParam>();
		
		public Dictionary<string,ActionParam> ParamList
		{
			get
			{
				return paramList;
			}
		}
		
		public string Name
		{
			get
			{
				return Prototype.Name;
			}
		}
		
		public Action(ActionPrototype prototype)
		{
			Prototype = prototype;
			foreach(KeyValuePair<string, Type> type in prototype.ParamTypes)
			{
				paramList[type.Key] =(ActionParam)Activator.CreateInstance(type.Value);
			}
		}
		
		public Action Clone()
		{
			Action action = (Action)this.MemberwiseClone();
			action.paramList = new Dictionary<string,ActionParam>();
			foreach(KeyValuePair<string, ActionParam> param in paramList)
			{
				action.paramList.Add(param.Key,param.Value.Clone());
			}
			return action;
		}
		
		public void ShowEditForm()
		{
			Prototype.ShowEditForm(this);
		}
	}
	
	//important: only use value types in derived classes!
	public class ActionParam
	{		
		public virtual string DisplayValue
		{
			get
			{
				return "Unknown";
			}
		}
		public virtual string CodeValue
		{
			get
			{
				return "Unknown";
			}
		}
		public virtual object Value
		{
			set
			{;}
		}
		
		public ActionParam Clone()
		{
			return (ActionParam)this.MemberwiseClone();
		}
		
		public virtual void WriteXml(XmlTextWriter xml)
		{
			throw new NotImplementedException();
		}
		
		public virtual void ReadXml(XmlElement element)
		{
			throw new NotImplementedException();
		}
	}
	
	public class ActionParamString : ActionParam
	{
		string val = "";
		
		public override string DisplayValue
		{
			get
			{
				return val;
			}
		}
		public override string CodeValue
		{
			get
			{
				return val;
			}
		}
		public override object Value
		{
			set
			{
				val = value as string;
			}
		}
		public override void WriteXml(XmlTextWriter xml)
		{
			xml.WriteString(val);
		}
		
		public override void ReadXml(XmlElement element)
		{
			val = element.InnerText;
		}
	}
}
